package com.gitee.pifeng.monitoring.ui.business.web.controller;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.pifeng.monitoring.ui.business.web.annotation.OperateLog;
import com.gitee.pifeng.monitoring.ui.business.web.entity.XjDataLink;
import com.gitee.pifeng.monitoring.ui.business.web.service.IXjJTopoService;
import com.gitee.pifeng.monitoring.ui.business.web.vo.LayUiAdminResultVo;
import com.gitee.pifeng.monitoring.ui.business.web.vo.XjDataLinkVo;
import com.gitee.pifeng.monitoring.ui.constant.OperateTypeConstants;
import com.gitee.pifeng.monitoring.ui.constant.UiModuleConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@Tag(name = "拓扑图")
@RequestMapping("/jtopo")
public class XjJTopoController {

    @Autowired
    private IXjJTopoService xjJTopoService;

    @Operation(summary = "新建数据链路页面")
    @GetMapping("/JTopoView")
    public ModelAndView JTopoView() {
        return new ModelAndView("jtopo/jtopo");
    }

    @Operation(summary = "链路列表页面")
    @GetMapping("/jtopoList")
    public ModelAndView jtopoList() {
        return new ModelAndView("jtopo/jtopoList");
    }

    @Operation(summary = "链路测试页面")
    @GetMapping("/test")
    public ModelAndView test() {
        return new ModelAndView("visio");
    }


    @Operation(summary = "链路详情页面")
    @GetMapping("/jtopoDetail")
    public ModelAndView jtopoDetail() {
        return new ModelAndView("jtopo/jtopoDetail");
    }

    public static void main(String[] args) {
        String json = "[{\"ldId\":null,\"fromNode\":\"源\",\"toNode\":null,\"dbId\":null,\"nodeX\":217,\"nodeY\":337,\"nodeRemark\":null,\"linkColor\":null,\"linkType\":null,\"linkRemark\":null,\"createTime\":\"2023-11-10T11:01:32.000+08:00\",\"dataLinkName\":null,\"links\":null},{\"ldId\":null,\"fromNode\":\"源\",\"toNode\":\"源1\",\"dbId\":null,\"nodeX\":null,\"nodeY\":null,\"nodeRemark\":null,\"linkColor\":\"blue\",\"linkType\":\"Link\",\"linkRemark\":null,\"createTime\":\"2023-11-10T11:01:33.000+08:00\",\"dataLinkName\":null,\"links\":null},{\"ldId\":null,\"fromNode\":\"源1\",\"toNode\":null,\"dbId\":null,\"nodeX\":350,\"nodeY\":295,\"nodeRemark\":null,\"linkColor\":null,\"linkType\":null,\"linkRemark\":null,\"createTime\":\"2023-11-10T11:01:33.000+08:00\",\"dataLinkName\":null,\"links\":null}]";
        Gson gson = new Gson();
        XjDataLink[] dataLinks = gson.fromJson(json, XjDataLink[].class);
        ArrayList<XjDataLink> data = new ArrayList<>();
        for (XjDataLink xjDataLink : dataLinks) {
            if(xjDataLink.getNodeX() != null){
                data.add(xjDataLink);
            }
        }

        for (XjDataLink xjDataLink : data) {
            ArrayList<XjDataLink> links = new ArrayList<>();
            for (XjDataLink link : dataLinks) {
                if(link.getFromNode().equals(xjDataLink.getFromNode()) && link.getNodeX() == null){
                    System.out.println("添加一条：" + link);
                    links.add(link);
                }
            }
            xjDataLink.setLinks(links);
        }

        System.out.println("data:" + data);
    }


    @Operation(summary = "获取数链列表")
    @Parameters(value = {
            @Parameter(name = "current", description = "当前页", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "size", description = "每页显示条数", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "dataLinkName", description = "数据链路名称", in = ParameterIn.QUERY)})
    @GetMapping("get-data-link-list")
    @ResponseBody
    public LayUiAdminResultVo getDataLinkList(@RequestParam(value = "current") Long current,
                                               @RequestParam(value = "size") Long size,
                                               @RequestParam(value = "dataLinkName", required = false) String dataLinkName,
                                               @RequestParam(value = "remark", required = false) String remark) {
        Page<XjDataLinkVo> page = this.xjJTopoService.getDataLinkList(current, size, dataLinkName, remark);
        return LayUiAdminResultVo.ok(page);
    }

    @Operation(summary = "获取数据链路详情")
    @ResponseBody
    @GetMapping("/getDataLinkDetail")
    public LayUiAdminResultVo getDataLinkDetail(@RequestParam(value = "dataLinkName") String dataLinkName) {
        return xjJTopoService.getDataLinkDetail(dataLinkName);
    }

    @Operation(summary = "创建数据链路")
    @ResponseBody
    @PostMapping("/createDataLink")
    public LayUiAdminResultVo createDataLink(@RequestParam(value = "dataLinkJson") String dataLinkJson,
                                             @RequestParam(value = "dataLinkName") String dataLinkName,
                                             @RequestParam(value = "remark") String remark) {
        ArrayList<XjDataLink> xjDataLinks = getInsterData(dataLinkJson, dataLinkName, remark);
        return xjJTopoService.addDataLink(xjDataLinks);
    }

    @Operation(summary = "删除数据链路")
    @DeleteMapping("/deleteDataLink")
    @ResponseBody
    @OperateLog(operModule = UiModuleConstants.DATABASE, operType = OperateTypeConstants.DELETE, operDesc = "删除数据链路")
    public LayUiAdminResultVo deleteDataLink(@RequestBody List<XjDataLink> xjDataLinks) {
        return xjJTopoService.deleteDataLink(xjDataLinks);
    }


    /**
     * 根据前端传进来的json转为对象数组
     * @param json
     * @return
     */
    public ArrayList<XjDataLink> getInsterData(String json, String dataLinkName, String remark){
        Gson gson = new Gson();
        XjDataLink[] xjDataLinks = gson.fromJson(json, XjDataLink[].class);
        ArrayList<XjDataLink> insterDataLink = new ArrayList<>();
        for (XjDataLink dataLink : xjDataLinks) {
            dataLink.setDataLinkName(dataLinkName);
            dataLink.setRemark(remark);
            insterDataLink.add(dataLink);
            ArrayList<XjDataLink> links = dataLink.getLinks();
            if(links.size()>0){
                for (int j = 0; j < links.size(); j++) {
                    links.get(j).setDataLinkName(dataLinkName);
                    links.get(j).setRemark(remark);
                    insterDataLink.add(links.get(j));
                }
            }
        }
        System.out.println("getDataLinkName():" + insterDataLink.get(0).getDataLinkName());
        System.out.println("insterDataLink.size():" + insterDataLink.size());
        return insterDataLink;
    }
}
